# -*- coding: utf-8 -*-            
# @Author : chenliang
# @Time : 2025/2/17 17:49
import time

from Common.dingtalk import Ding
from Common.zentao import Zentao
from datetime import datetime

token = Zentao().search_token()
#print(token)
result_list = []
for i in ['46','47']:
    result = Zentao().search_bugs(token=token,id=i,status='all')
    result_list.append(result)

str = Zentao().group_bugs_by_assignee_list(result_list)

# all = Zentao().search_bugs(token=token,status='all')
# str_all = Zentao().group_bugs_by_assignee(all)
#print(str)
# print('12')
develop_list = []
closed_list = []
for key in str:
    #print(key)
    # if key=='陈亮':
    #     result=Ding().generate_dingtalk_message(key,str[key])
    #     print(result)
    #     #Ding().send_to_dingtalk('https://oapi.dingtalk.com/robot/send?access_token=69084d85fde044caf8b3e12128779646cf1552d0606208923186cfabb275e211',result)
    #     print(23)

    if key=='崔林' or key=='何家豪' or key=='吴振' or key=='孙成':
        for a in str[key]:
            develop_list.append(a)
    if key=='closed':
        for a in str[key]:
            print(a)
            if a['closedDate'] == datetime.now().strftime("%Y-%m-%d"):
                closed_list.append(a)

if develop_list:
    develop_list = sorted(
        develop_list,
        key=lambda x: (
            {'P1': 0, 'P2': 1, 'P3': 2}[x['priority']],  # 优先级数字映射
            -int(x['create_time'].replace('-', ''))  # 日期转数字取反实现倒序
        )
    )
    result=Ding().generate_dingtalk_message(bugs=develop_list)
    Ding().send_to_dingtalk('https://oapi.dingtalk.com/robot/send?access_token=69084d85fde044caf8b3e12128779646cf1552d0606208923186cfabb275e211',result)

time.sleep(5)

if closed_list:
    result_closed =Ding().closed_dingtalk_message(bugs=closed_list)
    Ding().send_to_dingtalk('https://oapi.dingtalk.com/robot/send?access_token=69084d85fde044caf8b3e12128779646cf1552d0606208923186cfabb275e211',result_closed)

# print(result)
# print(result_closed)
# Ding().send_to_dingtalk('https://oapi.dingtalk.com/robot/send?access_token=69084d85fde044caf8b3e12128779646cf1552d0606208923186cfabb275e211',result)
# time.sleep(5)
# Ding().send_to_dingtalk('https://oapi.dingtalk.com/robot/send?access_token=69084d85fde044caf8b3e12128779646cf1552d0606208923186cfabb275e211',result_closed)

